using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SkinableFormsLib
{
	public class LWCheckBox : CheckBox, ILWControl
	{

		internal Bitmap _lwBitmap;
		internal Rectangle _lwRectangle;
		internal byte rc;
		internal byte gc;
		internal byte bc;

		internal Color _lwDesiredBackColor = CheckBox.DefaultBackColor;
		internal bool _lwTransparency = false;
		internal bool _lwTransparent = !false;
		internal Color _lwTransparentColor = Color.FromArgb(255, 3, 3, 3);


		public LWCheckBox()
			: base()
		{
			_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			_lwDesiredBackColor = base.BackColor;

		}

		internal bool _lwDragForm = false;
		public bool LWDragForm
		{
			get
			{
				return _lwDragForm;
			}
			set
			{
				_lwDragForm = value;
			}
		}
		/// <summary>
		/// Recomputes the internal Bitmap size
		/// </summary>
		/// <param name="e"></param>
		protected override void OnResize(System.EventArgs e)
		{
			if (_lwBitmap == null ||
				_lwBitmap.Width != Width || _lwBitmap.Height != Height)
			{

				if (Width != 0 && Height != 0)
				{
					DisposeInternal();
					_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
					_lwRectangle = new Rectangle(0, 0, _lwBitmap.Width, _lwBitmap.Height);
				}
			}
		}




		

		protected override void OnPaint(PaintEventArgs pevent)
		{
			if (!DesignMode)
				_lwTransparency = Utils.GetTransparency(this);
			base.OnPaint(pevent);
			if (!DesignMode)
			{
				if (_lwBitmap == null)
				{
					OnResize(null);
				}
				
				

				this.DrawToBitmap(_lwBitmap, _lwRectangle);

				if (_lwTransparency)
				{
					Utils.BitmapMakeTransparent(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc);
					//or
					//Utils.BitmapMakeTransparentUnsafe(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc); //I suspect this one is faster

				}
			}
		}







		private void DisposeInternal()
		{

			if (_lwBitmap != null)
				_lwBitmap.Dispose();

		}








		#region ILWControl Members

		Bitmap ILWControl.LWBitmap
		{
			get
			{

				return _lwBitmap;
			}
		}

		public Color LWDesiredBackColor
		{
			get
			{
				return _lwDesiredBackColor;
			}
			set
			{
				_lwDesiredBackColor = value;
			}
		}

		public bool LWTransparent
		{
			get
			{
				return _lwTransparent;
			}
			set
			{
				_lwTransparent = value;
			}
		}

		public bool LWTransparency
		{
			get
			{
				return _lwTransparency;
			}
		}


		public Color LWTransparentColor
		{
			get
			{
				return _lwTransparentColor;
			}
			set
			{
				_lwTransparentColor = value;
				rc = _lwTransparentColor.R;
				gc = _lwTransparentColor.G;
				bc = _lwTransparentColor.B;
			}
		}


		#endregion

	}
}
